package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.allen.library.SuperButton;
import com.yiwei.ydd.MainActivity;
import com.yiwei.ydd.R;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.fragment.GuideFragment;
import com.yiwei.ydd.util.PreferenceUtils;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.CustomViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class GuideActivity extends BaseActivity {


    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.layout_indicator)
    LinearLayout layoutIndicator;
    @BindView(R.id.btn_go)
    SuperButton btnGo;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        PreferenceUtils.putBoolean(this, "first_guide", false);
        GuideFragment fragment1 = new GuideFragment();
        fragment1.setPoi(0);
        GuideFragment fragment2 = new GuideFragment();
        fragment2.setPoi(1);
        GuideFragment fragment3 = new GuideFragment();
        fragment3.setPoi(2);
        Fragment[] fragments = new Fragment[]{fragment1, fragment2, fragment3};
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < fragments.length; i++) {
                    ((ImageView) layoutIndicator.getChildAt(i)).setImageResource(R.drawable.img_indicator_uncheck);
                }
                ((ImageView) layoutIndicator.getChildAt(position)).setImageResource(R.drawable.img_indicator_checked);

                if (position == 2) {
                    btnGo.setVisibility(View.VISIBLE);
                } else {
                    btnGo.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        for (int i = 0; i < fragments.length; i++) {
            ImageView view = (ImageView) LayoutInflater.from(this).inflate(R.layout.indicator_check, null);
            if (i == 0) {
                view.setImageResource(R.drawable.img_indicator_checked);
            }
            layoutIndicator.addView(view);
        }
        for (int i = 0; i < layoutIndicator.getChildCount(); i++) {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) ((ImageView) layoutIndicator.getChildAt(i)).getLayoutParams();
            lp.setMargins(6, 6, 6, 6);
            ((ImageView) layoutIndicator.getChildAt(i)).setLayoutParams(lp);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_go)
    public void onViewClicked() {
        if(btnGo.getVisibility()==View.VISIBLE){
            Util.startActivity(this, MainActivity.class);
            finish();
        }
    }
}
